var express = require('express');
var app = express();
var mysql = require('mysql');
var fs = require('fs');
var qs = require('querystring');
app.use(express.static('project-1'));
var data;
var tableId =0;
//链接数据库
var pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: '666666',
    database: 'project-1',
    port: 3306
});

var query=function(sql,callback){
    pool.getConnection(function(err,conn){
        if(err){
            callback(err,null,null);
        }else{
            conn.query(sql,function(qerr,vals,fields){
                //释放连接
                conn.release();
                //事件驱动回调
                callback(qerr,vals,fields);
            });
        }
    });
};
query("SELECT * FROM `project-1`.`pro-1data`",function(err,vals,fields){
    //do something
    if(err){
        throw err;
    }

    console.log(vals.length);
    tableId = vals.length;
});
//路由切换
app.get('/index (noloaded).html',function (req,res) {
    if(req.url === '/index (noloaded).html'){
        fs.readFile('index (noloaded).html',function(err,data){
            if(err) throw err;
            res.writeHead(200,{"Content-Type":"text/html"});
            res.end(data.toString());
        });
    }else{
        fs.readFile('404T.T.html',function(err,data){
            if(err) throw err;
            res.writeHead(200,{"Content-Type":"text/html"});
            res.end(data.toString());
        });
    }
})
//注册
app.post('/register-success.html',function (req,res) {
    var postData = '';
    tableId++;
    req.addListener('data',function (dataChunk) {
        postData += dataChunk;
    });
    req.addListener('end',function () {
        data = qs.parse(postData);
        console.log(data);
        query("INSERT INTO `project-1`.`pro-1data` (`id`, `username`, `password`, `phoneNum`) VALUES ('"+tableId+"', '"+data.username+"', '"+data.password2+"', '"+data.phoneNum2+"')",function(err,vals,fields){
            if(err){
                throw err;
            }
            console.log(vals);

        });
        fs.readFile('project-1/register-success.html',function (err,data) {
            if(err){
                fs.readFile('project-1/register-defeat.html',function (err,data) {
                    if(err) throw err;
                    res.writeHead(200,{"Content-Type":"text/html"});
                    res.end(data.toString());
                })
            }else{
                res.writeHead(200,{"Content-Type":"text/html"});
                res.end(data.toString());
            }
        })

    })
});

//登录
app.post('/personal_center.html',function (req,res) {
    var postData = '';
    var sendMsg;
    req.addListener('data',function (postChunk) {
        postData += postChunk;
    });
    req.addListener('end',function () {
        var json = qs.parse(postData);
        console.log(json);
        query("SELECT * FROM `project-1`.`pro-1data` where phoneNum = '"+json.phoneNum+"' and password = '"+json.password+"'",function(err,vals,fields){
            if(err){
                throw err;
            }
            console.log(vals[0].username);
            sendMsg = vals;
            fs.readFile('project-1/personal_center.html',function (err,data) {
                if(err){
                    fs.readFile('project-1/404T.T.html',function (err,data) {
                        if(err) throw err;
                        res.writeHead(200,{"Content-Type":"text/html"});
                        res.end(data.toString());
                    })
                }else{
                    res.writeHead(200,{"Content-Type":"text/html"});
                    res.end(data.toString());
                }
            })
        });
    })
})



app.listen(3000);